﻿using System;

namespace SSAT.Utils
{
    /// <summary>
    /// Provides methods for common verification like
    /// check argument for null and throw an exception, etc.
    /// </summary>
    public static class Verification
    {
        private static void CheckParameterName(string argumentName)
        {
            if (string.IsNullOrEmpty(argumentName))
            {
                throw new ArgumentException(Resources.ArgumentNameMustBeSpecified);
            }
        }

        /// <summary>
        /// Checks whether specified argument is null. If so then 
        /// System.ArgumentNullException will be thrown.
        /// </summary>
        /// <typeparam name="T">Type of the argument. Only reference types.</typeparam>
        /// <param name="argument">Argument to check.</param>
        /// <param name="argumentName">The name of the argument. This parameter can't 
        /// be null or empty. Otherwise System.ArgumentException will be thrown.</param>
        /// <exception cref="System.ArgumentException"></exception>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static void CheckArgumentForNull<T>(T argument, string argumentName) 
            where T : class
        {
            CheckParameterName(argumentName);
            if (argument == null)
            {
                throw new ArgumentNullException(argumentName);
            }
        }

        /// <summary>
        /// Checks whether specified method's string-type argument is null or empty. 
        /// If so then System.ArgumentException will be thrown.
        /// </summary>
        /// <param name="argument">String to check.</param>
        /// <param name="argumentName">The name of the argument. This parameter can't 
        /// be null or empty. Otherwise System.ArgumentException will be thrown.</param>
        /// <param name="message">Message to assign to the exception.</param>
        /// <exception cref="System.ArgumentException"></exception>
        public static void CheckStringForNullOrEmpty(string argument, string argumentName, string message)
        {
            CheckParameterName(argumentName);
            if (string.IsNullOrEmpty(argument))
            {
                throw new ArgumentException(message, argumentName);
            }
        }

        /// <summary>
        /// Checks whether specified method's string-type argument is null or empty. 
        /// If so then System.ArgumentException will be thrown.
        /// </summary>
        /// <param name="argument">String to check.</param>
        /// <param name="argumentName">The name of the argument. This parameter can't 
        /// be null or empty. Otherwise System.ArgumentException will be thrown.</param>
        /// <exception cref="System.ArgumentException"></exception>
        public static void CheckStringForNullOrEmpty(string argument, string argumentName)
        {
            CheckStringForNullOrEmpty(argument, argumentName, string.Format(Resources.ArgumentCantBeNullOrEmpty, argumentName));
        }
    }
}
